package com.white.movies.Activities;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.white.movies.Adapters.FilmListAdapter;
import com.white.movies.Adapters.SlidersAdapter;
import com.white.movies.Domains.MoviesBean;
import com.white.movies.Utils.JsonUtils;
import com.white.movies.Utils.Tools;
import com.white.movies.databinding.ActivityMainBinding;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    private Handler sliderHandler = new Handler();
    private Runnable silderRunnable = new Runnable() {
        @Override
        public void run() {
            binding.viewPager2.setCurrentItem(binding.viewPager2.getCurrentItem() + 1);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        MoviesBean movieFromJson = JsonUtils.getMovieFromJson(this, "movies.json");

        initBanner(movieFromJson.getBanners());
        initTopMoving(movieFromJson.getItems());
        initUpcoming(movieFromJson.getUpcommings());
    }

    private void initBanner(ArrayList<MoviesBean.Banner> items) {
        binding.progressBarBanner.setVisibility(View.VISIBLE);
        banners(items);
        binding.progressBarBanner.setVisibility(View.GONE);
    }


    private void initTopMoving(ArrayList<MoviesBean.Item> items) {
        binding.progressBarTop.setVisibility(View.VISIBLE);
        binding.recyclerViewTopMovies.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
        FilmListAdapter<MoviesBean.Item> adapter = new FilmListAdapter<>(
                items,
                new FilmListAdapter.OnBindDataListener<MoviesBean.Item>() {
                    @Override
                    public void onBind(FilmListAdapter.Viewholder holder, MoviesBean.Item item, int position, Context context) {
                        holder.titleTxt.setText(item.getTitle());
                        Tools.setImage(item.getPoster(), holder.pic, context);
                    }
                }
        );
        adapter.setOnItemClickListener((position, item) -> {
            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
            intent.putExtra("object", item);
            startActivity(intent);
        });
        binding.recyclerViewTopMovies.setAdapter(adapter);
        binding.progressBarTop.setVisibility(View.GONE);
    }


    private void initUpcoming(ArrayList<MoviesBean.Upcomming> items) {
        binding.progressBarUpcoming.setVisibility(View.VISIBLE);
        binding.recyclerViewUpcoming.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
        FilmListAdapter<MoviesBean.Upcomming> adapter = new FilmListAdapter<>(
                items,
                new FilmListAdapter.OnBindDataListener<MoviesBean.Upcomming>() {
                    @Override
                    public void onBind(FilmListAdapter.Viewholder holder, MoviesBean.Upcomming item, int position, Context context) {
                        holder.titleTxt.setText(item.getTitle());
                        Tools.setImage(item.getPoster(), holder.pic, context);
                    }
                }
        );
        adapter.setOnItemClickListener((position, item) -> {
        });
        binding.recyclerViewUpcoming.setAdapter(adapter);
        binding.progressBarUpcoming.setVisibility(View.GONE);
    }

    private void banners(ArrayList<MoviesBean.Banner> items) {
        binding.viewPager2.setAdapter(new SlidersAdapter(items, binding.viewPager2));
        binding.viewPager2.setClipToPadding(false);
        binding.viewPager2.setClipChildren(false);
        binding.viewPager2.setOffscreenPageLimit(3);
        binding.viewPager2.getChildAt(0).setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);

        CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
        compositePageTransformer.addTransformer(new MarginPageTransformer(40));
        compositePageTransformer.addTransformer(new ViewPager2.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                float r = 1 - Math.abs(position);
                page.setScaleY(0.85f + r * 0.15f);
            }
        });
        binding.viewPager2.setPageTransformer(compositePageTransformer);
        binding.viewPager2.setCurrentItem(1);
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                sliderHandler.removeCallbacks(silderRunnable);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        sliderHandler.removeCallbacks(silderRunnable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sliderHandler.postDelayed(silderRunnable, 2000);
    }
}
